﻿/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2023  Yi Zhang (yizhang-geo@zju.edu.cn)
 *
 * GCTL is distributed under a dual licensing scheme. You can redistribute 
 * it and/or modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation, either version 2 
 * of the License, or (at your option) any later version. You should have 
 * received a copy of the GNU Lesser General Public License along with this 
 * program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * If the terms and conditions of the LGPL v.2. would prevent you from using 
 * the GCTL, please consider the option to obtain a commercial license for a 
 * fee. These licenses are offered by the GCTL's original author. As a rule, 
 * licenses are provided "as-is", unlimited in time for a one time fee. Please 
 * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget 
 * to include some description of your company and the realm of its activities. 
 * Also add information on how to contact you by electronic and paper mail.
 ******************************************************/

#include "stream.h"

#include "cmath"

#if defined _WINDOWS || __WIN32__
#include "io.h"
// Test for file existence
#define F_OK 0
#endif

//在终端显示一个简易的GCTL图标
void gctl::display_logo(std::ostream &sout)
{
	sout << "   ___   ___  _____   __\n";
	sout << "  / _ \\ / __\\/__   \\ / /\n";
	sout << " / /_\\// /     / /\\// /\n";
	sout << "/ /_\\\\/ /___  / /  / /___\n";
	sout << "\\____/\\____/  \\/   \\____/\n";
	sout << "Geophysical Computational Tools & Library\n";
	sout << "Author: Dr. Yi Zhang (yizhang-geo@zju.edu.cn)\n\n";
	return;
}

//替换str中所有lod_value为new_value,返回被替换的old_value的个数
int gctl::replace_all(std::string& new_str, const std::string& old_str,const std::string& old_value,const std::string& new_value)
{
	int count = 0;
	new_str = old_str;
	for(std::string::size_type pos(0);pos!=std::string::npos;pos+=new_value.length())
	{
		if((pos=new_str.find(old_value,pos))!=std::string::npos)
		{
			new_str.replace(pos,old_value.length(),new_value);
			count++;
		}
		else break;
	}
	return count;
}

//在输入字符串末尾添加一段字符串，如果输入字符串的尾端与待添加的字符串一致则不添加并返回原字符串
std::string gctl::patch_string(std::string in_str, std::string patch_str)
{
	int inlen = in_str.length();
	int exlen = patch_str.length();

	std::string out_str = in_str;

	if (exlen > inlen)
	{
		out_str += patch_str;
		return out_str;
	}

	if (exlen == inlen && in_str != patch_str)
	{
		out_str += patch_str;
		return out_str;
	}
	
    if (in_str.substr(inlen - exlen, inlen) != patch_str)
    {
        out_str += patch_str;
		return out_str;
    }

	return out_str;
}

//转换string对象为stringstream对象
void gctl::str2ss(std::string in_str, std::stringstream &out_ss, std::string delimiter)
{
	if (delimiter != "")
	{
		std::string replace_str;
		replace_all(replace_str, in_str, delimiter, " ");
		out_ss.clear();
		out_ss.str(replace_str);
	}
	else
	{
		out_ss.clear();
		out_ss.str(in_str);
	}
	return;
}

/**
 * @brief      转换string字符串为double类型的数值
 * 
 * 这个函数的主要作用是检查输入字符串是否为nan或者inf等表示无效值的符号。有的编译器
 * 可以在>>输入符中完成此检测，但为了函数功能的稳定，所以在此处自定了这个函数。
 *
 * @param[in]  instr  输入字符串
 *
 * @return     返回的double类型的数值
 */
double gctl::str2double(std::string instr)
{
	if (instr == "NAN" || instr == "nan" || instr == "NaN")
		return NAN;
	else if (instr == "INF" || instr == "inf" || instr == "Inf")
		return INFINITY;
	else
	{
		auto e(instr.find_first_of("dD"));
		if (e != std::string::npos) instr[e] = 'e';

		return atof(instr.c_str());
	}
}

/**
 * @brief      转换double类型数值为string类型字符串
 *
 * @param[in]  in_d  输入数值
 *
 * @return     输出字符串
 */
std::string gctl::double2str(double in_d)
{
	std::string tmp_str;
	std::stringstream tmp_ss;

	tmp_ss.clear();
	if (std::isnan(in_d))
		tmp_ss.str("nan");
	else if (std::isinf(in_d))
		tmp_ss.str("inf");
	else tmp_ss << in_d;

	tmp_ss >> tmp_str;
	return tmp_str;
}

//返回指定长度的随机字符串
void gctl::random_char(unsigned int length, char* out)
{
	char str[76] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*+_-=?";
	int i,lstr;
	char ss[2] = {0};
	lstr = strlen(str);//计算字符串长度
	srand((unsigned int)time((time_t *)NULL));//使用系统时间来初始化随机数发生器
	//按指定大小返回相应的字符串
	for(i = 1; i <= length; i++)
	{
		snprintf(ss, 2, "%c",str[(rand()%lstr)]);//rand()%lstr 可随机返回0-61之间的整数, str[0-61]可随机得到其中的字符
		strcat(out,ss);//将随机生成的字符串连接到指定数组后面
	}
	return;
}

void gctl::random_str(unsigned int length, std::string &out_str)
{
	char *out_char = new char [length];
	random_char(length, out_char);
	out_str  = out_char;
	delete[] out_char;
	return;
}

//测试打开输入文件 如果成功则返回0并输出信息 否则返回1
void gctl::open_infile(std::ifstream &infile, std::string filename, std::string extension, 
	std::ios_base::openmode mode)
{
	if (filename == "")
	{
		throw domain_error("Empty file name. From open_infile(...)");
	}

	// 文件流开启则先关闭文件
	if (infile.is_open()) infile.close();
	// 检查文件后缀名是否与所给后缀一致 若无制定后缀名则跳过
	if (extension != "")
	{
		int exlen = extension.length();
		int filen = filename.length();
		// 文件名无后缀或者后缀名与制定类型不一致
		if (exlen >= filen || filename.substr(filen-exlen, filen) != extension)
		{
			// 1以指定后缀名打开文件
			infile.open(filename+extension, mode);
			if (!infile)
			{
				throw domain_error("Fail to open file: " + filename + extension + ". From open_infile(...)");
			}
			return;
		}
	}

	// 无指定后缀名或者文件名包含后缀名 直接打开文件
	infile.open(filename, mode);
	if (!infile)
	{
		throw domain_error("Fail to open file: " + filename + ". From open_infile(...)");
	}
	return;
}

void gctl::open_matched_infile(std::ifstream &infile, std::string filename, 
	std::string exten_pattern, std::ios_base::openmode mode)
{
	if (exten_pattern == "" || filename == "")
	{
		throw domain_error("Empty file name or extension(s). From open_matched_infile(...)");
	}

	if (infile.is_open()) infile.close();

	std::vector<std::string> extens;
	parse_string_to_vector(exten_pattern, ',', extens);

	int exlen, filen = filename.length();
	for (int i = 0; i < extens.size(); ++i)
	{
		exlen = extens[i].length();
		if (filename.substr(filen-exlen, filen) == extens[i])
		{
			infile.open(filename, mode);
			if (!infile)
			{
				throw domain_error("Fail to open file: " + filename + ". From open_matched_infile(...)");
			}
			return;
		}
	}

	// 没有找到包含允许的后缀名的文件 开始依次添加后缀名并尝试打开文件
	for (int i = 0; i < extens.size(); ++i)
	{
		if (access((filename+extens[i]).c_str(), F_OK) != -1)
		{
			infile.open(filename+extens[i], mode);
			if (!infile)
			{
				throw domain_error("Fail to open file: " + filename + extens[i] + ". From open_matched_infile(...)");
			}
			return;
		}
	}

	throw domain_error("Fail to open file: " + filename + ". Desired file extension: " + exten_pattern + ". From open_matched_infile(...)");
	return;
}

//测试打开输出文件 如果成功则返回0并输出信息 否则返回1
void gctl::open_outfile(std::ofstream &outfile, std::string filename, std::string extension, 
	std::ios_base::openmode mode)
{
	if (filename == "")
	{
		throw domain_error("Empty file name. From open_outfile(...)");
	}

	// 文件流开启则先关闭文件
	if (outfile.is_open()) outfile.close();
	// 检查文件后缀名是否与所给后缀一致 若无制定后缀名则跳过
	if (extension != "")
	{
		int exlen = extension.length();
		int filen = filename.length();
		// 文件名无后缀或者后缀名与制定类型不一致
		if (exlen >= filen || filename.substr(filen-exlen, filen) != extension)
		{
			// 1以指定后缀名打开文件
			outfile.open(filename+extension, mode);
			if (!outfile)
			{
				throw domain_error("Fail to create file: " + filename + extension + ". From open_outfile(...)");
			}
			return;
		}
	}

	// 无指定后缀名或者文件名包含后缀名 直接打开文件
	outfile.open(filename, mode);
	if (!outfile)
	{
		throw domain_error("Fail to create file: " + filename + ". From open_outfile(...)");
	}
	return;
}

void gctl::open_matched_outfile(std::ofstream &outfile, std::string filename, 
	std::string exten_pattern, std::ios_base::openmode mode)
{
	if (exten_pattern == "" || filename == "")
	{
		throw domain_error("Empty file name or extension(s). From open_matched_outfile(...)");
	}

	if (outfile.is_open()) outfile.close();

	std::vector<std::string> extens;
	parse_string_to_vector(exten_pattern, ',', extens);

	int exlen, filen = filename.length();
	for (int i = 0; i < extens.size(); ++i)
	{
		exlen = extens[i].length();
		if (filename.substr(filen-exlen, filen) == extens[i])
		{
			outfile.open(filename, mode);
			if (!outfile)
			{
				throw domain_error("Fail to create file: " + filename + ". From open_matched_outfile(...)");
			}
			return;
		}
	}

	// 没有找到包含允许的后缀名的文件 开始依次添加后缀名并尝试打开文件
	for (int i = 0; i < extens.size(); ++i)
	{
		if (access((filename+extens[i]).c_str(), F_OK) != -1)
		{
			outfile.open(filename+extens[i], mode);
			if (!outfile)
			{
				throw domain_error("Fail to create file: " + filename + extens[i] + ". From open_matched_outfile(...)");
			}
			return;
		}
	}

	throw domain_error("Fail to create file: " + filename + ". Desired file extension: " + exten_pattern + ". From open_matched_outfile(...)");
	return;
}
